import constant from "@/public/constant";
export default {
  state: {
    pageCode: "",
    fromPage: "", //从何页面进入
    SRMindexData: "",
    accountQuota: 0,
  },
  getters: {
    changePageCodeStatus: (state) => {
      return state.pageCode;
    },
    changePageSRMindexData: (state) => {
      return state.SRMindexData;
    },
  },
  mutations: {
    updatePageCode(state, { payload }) {
      state.pageCode = payload;
    },
    updateFromPage(state, { payload }) {
      state.fromPage = payload;
    },
    updateSRMindexData(state, { payload }) {
      state.SRMindexData = payload;
    },
    updateAccountQuota(state, { payload }) {
      state.accountQuota = payload;
    },
  },
  actions: {
    changePageCode({ commit }, payload) {
      commit({
        type: "updatePageCode",
        payload,
      });
    },
    changeFromPage({ commit }, payload) {
      commit({
        type: "updateFromPage",
        payload,
      });
    },
    changeSRMindexData({ commit }, payload) {
      commit({
        type: "updateSRMindexData",
        payload,
      });
    },
    changeAccountQuota({ commit }, payload) {
      let _this = this;
      return new Promise((resolve, reject) => {
        _this._vm.$http
          .getAvailableAmount({ supplierId: payload.supplierId })
          .then(({ data }) => {
            if (data.code == 200) {
              let amount = data.rows.sumBorrowBalance || 0.0;
              commit({
                type: "updateAccountQuota",
                payload: amount,
              });
              resolve();
            } else {
              reject(data.info);
            }
          })
          .catch((err) => {
            reject("获取对账融资额度失败,请检查网络!");
          });
      });
    },
  },
};
